
#include<math.h>

#include"types.h"
#include"paintShape.h"

#define Pi 3.141592654f

#define CIRCLE_POINTS_LIMIT		60
#define STAR_POINTS_LIMIT		30
#define VORTEX_DEPTH			10
#define VORTEX_POINTS_LIMIT		CIRCLE_POINTS_LIMIT * VORTEX_DEPTH

Vector2d	circle_points	[CIRCLE_POINTS_LIMIT];
Vector2d	star_points		[STAR_POINTS_LIMIT];
Vector2d	vortex_points	[VORTEX_POINTS_LIMIT];

/* preps and helps */
/*---------------------------------------------------------------------------------*/
float convertDegToRad(int a)
{
	return a*Pi/180.0f;
}

void prepUnitCircle()
{
	int i;
	// define points on unit cycle
	for( i=0 ; i<CIRCLE_POINTS_LIMIT ; ++i )
	{
		circle_points[i].x = sinf( convertDegToRad((i+1)*360/CIRCLE_POINTS_LIMIT) );
		circle_points[i].y = cosf( convertDegToRad((i+1)*360/CIRCLE_POINTS_LIMIT) );
	}
}

void prepStar()
{
	int i;
	for( i=0 ; i<STAR_POINTS_LIMIT ; ++i )
	{
		star_points[i].x = 
			sinf( convertDegToRad((i+1)*360/STAR_POINTS_LIMIT) ) * (1 + (i%2)*0.3f);

		star_points[i].y = 
			cosf( convertDegToRad((i+1)*360/STAR_POINTS_LIMIT) ) * (1 + (i%2)*0.3f);
	}
}

void prepVortex()
{
	int i, j, depth=10;
	float factor;

	for( j=0 ; j<depth ; ++j )
	{
		for( i=0 ; i<CIRCLE_POINTS_LIMIT; ++i )
		{
			factor = (j+((float)i/CIRCLE_POINTS_LIMIT))/depth;
			
			vortex_points[i+j*CIRCLE_POINTS_LIMIT].x = circle_points[i].x * factor;
			vortex_points[i+j*CIRCLE_POINTS_LIMIT].y = circle_points[i].y * factor;
		}
	}
}

void inits()
{
	prepStar();
	prepUnitCircle();
	prepVortex();
}
/*---------------------------------------------------------------------------------*/

/* paints */
/*---------------------------------------------------------------------------------*/
void paintEmptyEllipse()
{
	int i;

	glBegin(GL_LINE_LOOP);

	for( i=0 ; i<CIRCLE_POINTS_LIMIT ; i++ )
	{
		glVertex2f(circle_points[i].x, circle_points[i].y);
	}

	glEnd();
}

void paintFullEllipse()
{
	int i;

	glBegin(GL_POLYGON);

	for( i=0 ; i<CIRCLE_POINTS_LIMIT ; i++ )
	{
		glVertex2f(circle_points[i].x, circle_points[i].y);
	}

	glEnd();
}

void paintStar()
{
	int i;

	glBegin(GL_LINE_LOOP);

	for( i=0 ; i<STAR_POINTS_LIMIT ; i++ )
	{
		glVertex2f(star_points[i].x, star_points[i].y);
	}
	glEnd();
}

void paintVortex()
{
	int i;

	glBegin(GL_LINES);

	
	for( i=0 ; i<VORTEX_POINTS_LIMIT-1 ; ++i )
	{
		glVertex2f( vortex_points[i].x, vortex_points[i].y );
		glVertex2f( vortex_points[i+1].x, vortex_points[i+1].y );
	}

	glEnd();
}

void paintRect()
{
	glBegin(GL_LINE_LOOP);

	glVertex2f(  1, 1 );
	glVertex2f(  1,-1 );
	glVertex2f( -1,-1 );
	glVertex2f( -1, 1 );

	glEnd();
}

void paintShape ( Shape* shape )
{
	if( shape==NULL )
		return;

	glColor3f( shape->color.r, shape->color.g, shape->color.b );

	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
	glTranslatef( shape->pos.x, shape->pos.y, 0 );
	glScalef( shape->width*shape->scale, shape->scale, 1 );

	switch( shape->shape_type )
	{
	case EMPTY_ELLIPSE: paintEmptyEllipse();	break;
	case FULL_ELLIPSE:	paintFullEllipse();		break;
	case VORTEX:		paintVortex();			break;
	case RECTENGLE:		paintRect();			break;
	case STAR:			paintStar();			break;
	}

	glPopMatrix();
}
/*---------------------------------------------------------------------------------*/
